# -*-coding:utf-8-*-
import logging


class MyLog(logging.Logger):
    def __init__(self, name = "api", level = logging.INFO, file = None):
        # 继承父类的有init方法需要调用一下
        super().__init__(name, level)
        fmt = "%(asctime)s--%(levelname)s -[%(filename)s %(lineno)d]-----------%(message)s"
        my_fmt = logging.Formatter(fmt, datefmt = "%Y-%m-%d %H:%M:%S")
        control_output = logging.StreamHandler()
        control_output.setFormatter(my_fmt)
        if file:
            file_output = logging.FileHandler(file, encoding = "utf-8")
            file_output.setFormatter(my_fmt)
            self.addHandler(file_output)
        self.addHandler(control_output)


def error_log(log_message):
    log=MyLog()
    log.error(log_message)
    raise Exception(log_message)


if __name__ == "__main__":
    log = MyLog("api", file = "../log/log.txt")
    log.info("这是一个info")
